﻿using System;

namespace 进制学习
{
    public class Program
    {
       static public byte x;
       static public byte y;
       static public byte z;
       static public byte w;
       static public byte x1;
       static public byte y1;
       static public byte z1;
       static public byte w1;
        public static char[] chars = { 'A','B','C','D','E',' ','F'};
        static public int i = 1;
        static void Main(string[] args)
        {
            Console.WriteLine(chars);
            Console.WriteLine("{0}", ++i);
            i = 1;
            Console.WriteLine("{0}", i++);
            while (true)  // 循环输入IP地址
            {
                Console.Write("请输入一个IP地址: ");
                string ip = Console.ReadLine();
                string[] ipArr = ip.Split('.'); // 分割IP地址

                if (ipArr.Length != 4)
                {
                    Console.Clear();
                    Console.WriteLine("输入的IP地址格式不正确！请重输");
                    continue;
                }

                byte[] ipNum = new byte[4]; // 定义一个字节数组，用于存放IP地址的各个数字

                bool isValid = true;
                for (int i = 0; i < 4; i++)
                {
                    try
                    {
                        ipNum[i] = byte.Parse(ipArr[i]); // 将IP地址的各个数字转换为字节
                    }
                    catch (FormatException)
                    {
                        Console.Clear();
                        Console.WriteLine("输入的IP地址包含无效字符！请重输");
                        isValid = false;
                        break;
                    }
                }

                if (!isValid)
                {
                    continue;
                }

                // 定义字节变量分别存放IP地址的每一位
                x = ipNum[0];
                y = ipNum[1];
                z = ipNum[2];
                w = ipNum[3];

                Console.WriteLine($"{x},{y},{z},{w}");
                break;
            }

            while (true)
            {
                Console.Write("请输入一个子网掩码: ");
                string zwym = Console.ReadLine();
                string[] zwymArr = zwym.Split('.'); // 分割IP地址

                if (zwymArr.Length != 4)
                {
                    Console.Clear();
                    Console.WriteLine("输入的子网掩码格式不正确！请重输");
                    continue;
                }

                byte[] ipNum = new byte[4]; // 定义一个字节数组，用于存放IP地址的各个数字

                bool isValid = true;
                for (int i = 0; i < 4; i++)
                {
                    try
                    {
                        ipNum[i] = byte.Parse(zwymArr[i]); // 将IP地址的各个数字转换为字节
                    }
                    catch (FormatException)
                    {
                        Console.Clear();
                        Console.WriteLine("输入的子网掩码包含无效字符！请重输");
                        isValid = false;
                        break;
                    }
                }

                if (!isValid)
                {
                    continue;
                }

                // 定义字节变量分别存放IP地址的每一位
                x1 = ipNum[0];
                y1 = ipNum[1];
                z1= ipNum[2];
                w1 = ipNum[3];

                Console.WriteLine($"你的子网掩码是{x1},{y1},{z1},{w1} \n 你的IP地址是{x}.{y}.{z}.{w}");
                break;
            }
// 计算子网掩码对应的网络地址
            byte[] networkAddress = new byte[4];
            networkAddress[0] = (byte)(x & x1);
            networkAddress[1] = (byte)(y & y1);
            networkAddress[2] = (byte)(z & z1);
            networkAddress[3] = (byte)(w & w1);

            Console.WriteLine($"你的网络地址是{networkAddress[0]}.{networkAddress[1]}.{networkAddress[2]}.{networkAddress[3]}");

            // 计算子网掩码对应的广播地址
            byte[] broadcastAddress = new byte[4];
            broadcastAddress[0] = (byte)(x | (byte)(~x1));
            broadcastAddress[1] = (byte)(y | (byte)(~y1));
            broadcastAddress[2] = (byte)(z | (byte)(~z1));
            broadcastAddress[3] = (byte)(w | (byte)(~w1));

            Console.WriteLine($"你的广播地址是{broadcastAddress[0]}.{broadcastAddress[1]}.{broadcastAddress[2]}.{broadcastAddress[3]}");

            // 计算子网掩码对应的主机地址范围
            byte[] hostAddressRange = new byte[4];
            hostAddressRange[0] = (byte)(x1 ^ 255);
            hostAddressRange[1] = (byte)(y1 ^ 255);
            hostAddressRange[2] = (byte)(z1 ^ 255);
            hostAddressRange[3] = (byte)(w1 ^ 255);

            Console.WriteLine($"你的主机地址范围是 {networkAddress[0]}.{networkAddress[1]}.{networkAddress[2]}.{networkAddress[3]+1} ~ {broadcastAddress[0]}.{broadcastAddress[1]}.{broadcastAddress[2]}.{broadcastAddress[3]-1}，共有{hostAddressRange[0] * hostAddressRange[1] * hostAddressRange[2] * hostAddressRange[3]}个主机地址");

            Console.ReadKey();
        }
    }
}
